#!/usr/bin/env python

from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

import xml.dom.minidom


#if result.status_code == 200:
#  doSomethingWithResult(result.content)
  
#transportdata = fetch()


class Station:
    id = 0
    name = ""
    number = 0
    latitude = 0
    longitude = 0
    slotsavailable = 0
    bikesavailable = 0
    pos = 0
    district = ""

def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc

class TransportResult:
    content = {}
    status = 1
    
    def getStatus(self):
        #todo: through exception if content is unset
        statusnode = self.content.getElementsByTagName("status")
        status = statusnode.attributes["code"] 
        return status
        
    def setContent(self, xmlString):
        self.content = xml.dom.minidom.parseString(xmlString)
    
    def getStations(self):
        stations = []
        data = self.content.getElementsByTagName("data")[0]
        stationNodes = data.getElementsByTagName("station")
        for stationNode in stationNodes:
            station = Station()
            station.id = getText(stationNode.getElementsByTagName("id")[0].childNodes)
            station.name = getText(stationNode.getElementsByTagName("name")[0].childNodes) 
            station.number = getText(stationNode.getElementsByTagName("number")[0].childNodes) 
            station.latitude = getText(stationNode.getElementsByTagName("latitude")[0].childNodes) 
            station.longitude = getText(stationNode.getElementsByTagName("longitude")[0].childNodes) 
            station.slotsavailable = getText(stationNode.getElementsByTagName("slotsavailable")[0].childNodes) 
            station.bikesavailable = getText(stationNode.getElementsByTagName("bikesavailable")[0].childNodes) 
            station.pos = getText(stationNode.getElementsByTagName("pos")[0].childNodes) 
            station.district = getText(stationNode.getElementsByTagName("district")[0].childNodes) 
            stations.append(station)
        return stations
            
class MainHandler(webapp.RequestHandler):
    def get(self):
        url = "http://data.keolis-rennes.com/xml/?version=1.0&key=IWFFT9Q3GXETJNC&cmd=getstation"
        transportdata = urlfetch.fetch(url)
        content = TransportResult()
        content.setContent(transportdata.content)
        stations = content.getStations()      
        self.response.out.write(template.render('rennes.html', {'stations':stations}))
        #self.response.out.write('test')

    def post(self):
        self.response.out.write('posted')
        
def main():
    application = webapp.WSGIApplication([('/rennes', MainHandler)], debug = True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

